package cn.kys.modules.system.service.impl;

import cn.kys.modules.system.repository.InfoQueryRepository;
import cn.kys.modules.system.service.InfoQueryService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service

/**
 * @Author zhaoxw
 * @Description //TODO   这个都没用上 所有接口
 * @Date 13:27 2024/1/11
 * @return
 **/
public class InfoQueryServiceImpl implements InfoQueryService {

    @Autowired
    private InfoQueryRepository infoQueryRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    GridFsTemplate gridFsTemplate;

    /**
     * @return
     * @Author zhaoxw
     * @Description //TODO 查询空间数据
     * @Date 13:20 2024/1/10
     **/
    @Override
    public List queryByDraw(String json) {

        List landList = new LinkedList<>();
        Map<String, List<Map>> resultMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray addressCoordinate = jsonObject.getJSONArray("address_coordinate");
        String taskidStr = jsonObject.getString("taskid");
        Integer taskid = 0;
        if (null != taskidStr && taskidStr != null) {
            taskid = Integer.parseInt(taskidStr);
        }

        //手绘
        if (addressCoordinate.size() > 1) {
            for (Object o : addressCoordinate) {
                List tempList = new ArrayList();
                tempList = infoQueryRepository.queryByDraw(taskid, o.toString());
                landList.add(tempList);
            }
        } else {
            String addressCoordinateStr = addressCoordinate.toString().replace("[\"", "").replace("\"]", "");
            List tempList = infoQueryRepository.queryByDraw(taskid, addressCoordinateStr);
            landList.add(tempList);
        }

        return landList;

    }


    /**
     * @return
     * @Author zhaoxw
     * @Description //TODO  按照编号查询已经完成的任务
     * @Date 15:19 2024/1/10
     **/
    @Override
    public List queryById(String id) {
        return infoQueryRepository.queryById(Integer.parseInt(id));
    }

    /**
     * @Author zhaoxw
     * @Description //TODO  按照时间段查询已经完成的任务
     * @Date 8:08 2024/1/11
     * @return
     **/
    @Override
    public List queryByTime(LocalDateTime startTime, LocalDateTime endTime, String taskId) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedStartTime = startTime.format(formatter);
        String formattedEndTime = endTime.format(formatter);
        Criteria criteria = Criteria.where("metadata.time").gte(formattedStartTime).lte(formattedEndTime);

        Query query = new Query(criteria);
        List<JSONObject> jsonObjects = mongoTemplate.find(query, JSONObject.class, "fs.files");
        //查出来后再去postgrelsql数据据库t_subtask_geom中查对应的id
        Map resMap=new HashMap();
        List resList=new LinkedList();
        for (JSONObject jsonObject : jsonObjects) {
            Map metadataMap = (Map) jsonObject.get("metadata");
            String globle_edit_geoidStr = metadataMap.get("globle_edit_geoid").toString();
            String globle_subtaskidStr = metadataMap.get("globle_subtaskid").toString();
            Integer id=0;
            Integer subtaskid=0;
            if (null != globle_edit_geoidStr && !globle_edit_geoidStr.equals("")){
                id = Integer.parseInt(globle_edit_geoidStr);
                subtaskid = Integer.parseInt(globle_subtaskidStr);
                //这里id已经是唯一值，再加上子任务的id 应该可以做到同一任务下的唯一约束
                List<Map> list = infoQueryRepository.queryByIdandSubtaskId(id,subtaskid);
                resList.add(list);
            }
        }
        return resList;
    }



}
